package com.gl.emms.whale.alipay;




import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import javax.security.auth.PrivateCredentialPermission;

import com.gl.emms.client.android.EMMSPushManager;
import com.gl.emms.nio.mutual.ReplyBody;
import com.gl.emms.whale.R;
import com.gl.emms.whale.app.Constant;
import com.gl.emms.whale.app.MyApplication;
import com.gl.emms.whale.app.WhaleMonitorActivity;
import com.gl.emms.whale.db.MessageDB;
import com.gl.emms.whale.db.RechargeRecordDB;
import com.gl.emms.whale.ui.AccountInfoActivity;
import com.gl.emms.whale.ui.RechargeRecordActivity;
import com.gl.emms.whale.ui.TabActivity;
import com.gl.emms.whale.util.RechargeRecordEntity;
import com.gl.emms.whale.wxapi.WXPayEntryActivity;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class PayResultActivity extends WhaleMonitorActivity{
	private RechargeRecordEntity rechargeRecordEntity;
	private Button recharge_btn;
	private TextView rechargeResultText;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.pay_result);

		Button titleBackBtn = (Button)findViewById(R.id.TitleBackBtn);  
		titleBackBtn.setOnClickListener(new OnClickListener() {  
			public void onClick(View v) {  
				KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,  
						KeyEvent.KEYCODE_BACK);
				finish();
				//mActivity.onKeyDown(KeyEvent.KEYCODE_BACK, newEvent);  
			}  
		});
		recharge_btn = (Button)findViewById(R.id.rechargerecord_btn);  
		recharge_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				switch (v.getId()) {
				case R.id.rechargerecord_btn:
					Intent rechargeRecordActivity = new Intent(PayResultActivity.this,RechargeRecordActivity.class);  
					startActivity(rechargeRecordActivity);
					break;

				default:
					break;
				}
			}
		});



		rechargeResultText = (TextView) findViewById(R.id.rechargeresult_text);


		Intent intent =getIntent();
		/*取出Intent中附加的数据*/
		boolean state = intent.getBooleanExtra("state", false);
		if (state == true) {
			rechargeResultText.setText("您已支付成功，请等待为您充值");
			EMMSPushManager.requestSelectRecharge(PayResultActivity.this, MyApplication.getUserData().getIdIP(), MyApplication.getUserData().getMainroom());

		}else {
			rechargeResultText.setText("充值失败");
		}
	}

	
	@Override
	public void onReplyReceived(final ReplyBody reply) {
		super.onReplyReceived(reply);
		if (reply.getType().equals("recharge_response")) {
			hideProgressDialog();
			if(reply.get("rechargestate").equals("000000"))
			{
				showToask("充值成功");
			}else
			{
				showToask("充值失败");
			}
			rechargeRecordEntity.setState("0");
			RechargeRecordDB rechargeRecordDB = new RechargeRecordDB(this);
			rechargeRecordDB.saveRecord(rechargeRecordEntity);// 保存到数据库 
			rechargeRecordDB.close();
		}else if (reply.getType().equals("rechargesync_response")) {
			hideProgressDialog();
			if(reply.get("rechargestate").equals("000000"))
			{
				showToask("充值成功");
			}else
			{
				showToask("充值失败");
			}
		}else if (reply.getType().equals("selectcharge_response")) {
			hideProgressDialog();
			if(reply.get("state").equals("000000"))
			{
				String money = rechargeRecordEntity.getMoney();
				String balance= reply.get("recharge");
				String cardnum= MyApplication.getUserData().getCardnum();
				EMMSPushManager.requestRecharge(PayResultActivity.this,MyApplication.getUserData().getIdIP(),MyApplication.getUserData().getName(),MyApplication.getUserData().getMainroom(),rechargeRecordEntity.getBill(),money,balance,cardnum);
			}else
			{
				showToask("充值失败");
			}
		}
	}


	@Override
	public void initViews() {
		// TODO Auto-generated method stub
		
	}


}